package com.cnunicom.modules.basisdata.controller;

import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.cnunicom.modules.sys.controller.AbstractController;
import com.cnunicom.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cnunicom.modules.basisdata.entity.BasisDataEntity;
import com.cnunicom.modules.basisdata.service.BasisDataService;
import com.cnunicom.common.utils.PageUtils;
import com.cnunicom.common.utils.R;


/**
 * 基础数据表
 *
 * @author dingziyuan
 * @email dzy998877@163.com
 * @date 2019-06-17 10:45:52
 */
@RestController
@RequestMapping("basisdata/basisdata")
public class BasisDataController extends AbstractController {
    @Autowired
    private BasisDataService basisDataService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("basisdata:basisdata:list")
    public R list(@RequestParam Map<String, Object> params) {
        params.put("user", getUser());
        PageUtils page = null;
        try {
            page = basisDataService.queryPage(params);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("basisdata:basisdata:info")
    public R info(@PathVariable("id") Long id) {
        BasisDataEntity basisData = basisDataService.getById(id);

        return R.ok().put("basisData", basisData);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("basisdata:basisdata:save")
    public R save(@RequestBody BasisDataEntity basisData) {
        Date date = new Date();
        basisData.setCreateTime(date);
        basisData.setUpdateTime(date);
        basisData.setCreateUserId(getUserId());
        basisDataService.save(basisData);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("basisdata:basisdata:update")
    public R update(@RequestBody BasisDataEntity basisData) {
        basisData.setUpdateTime(new Date());
        basisDataService.updateById(basisData);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("basisdata:basisdata:delete")
    public R delete(@RequestBody Long[] ids) {
        basisDataService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }


}
